#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N=1e6+10;
vector<int> arr;
vector<pair<int,int>> edge[N];
int dist[N];
int st[N];
void spfa()
{
	memset(dist,-0x3f3f3f,sizeof(dist));
	dist[0]=0;
	queue<int> q;
	q.push(0);
	st[0]=1;
	while(q.size()){
		int r=q.front();
		st[r]=0;
		q.pop();
		for(auto& e:edge[r])
		{
			int aim=e.first;
			int step=e.second;
			if(dist[aim]<dist[r]+step)
			{
				dist[aim]=dist[r]+step;
				if(st[aim]==0)
				{
					q.push(aim);
					st[aim]=1;
				}
			
			}
		}
	}
}
int main()
{
	arr.push_back(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		edge[0].push_back({i,1});
	}
	
	for(int i=1;i<=n;i++)
	{
		int t;
		cin>>t;
		arr.push_back(t);
	}
	for(int i=2;i<=n;i++)
	{
		int a=arr[i-1],b=arr[i];
		//b>=a+1
		if(b>a)
		{
			edge[i-1].push_back({i,1});
		}
		//a>=b+1
		else if(b<a)
		{
			edge[i].push_back({i-1,1});
		}
		else{
			edge[i].push_back({i-1,0});
			edge[i-1].push_back({i,0});
		}
	}
	
	spfa();
	int ret=0;
	for(int i=1;i<=n;i++)
	{
		ret+=dist[i];
	}
	cout<<ret<<endl;
	return 0;
}
